<template>
  <div>
    <div class="search-wrap">
      <el-input placeholder="请输入文章标题" class="search-input" v-model="keyWord">
        <template slot="append">
          <el-button type="primary" @click="onSearch">查询</el-button>
        </template>
      </el-input>
      <el-button type="primary" @click="addArticle">新建文章</el-button>
    </div>

    <el-table border :data="tableData" style="width: 100%">
      <el-table-column align="center" type="index" label="序号" width="60"></el-table-column>
      <el-table-column align="center" prop="title" label="标题"></el-table-column>
      <el-table-column align="center" prop="classify" label="分类"></el-table-column>
      <el-table-column align="center" prop="tags" label="标签">
        <template slot-scope="scope">
          <el-tag size="small" v-for="(item,k) in scope.row.tags" :key="k">{{item}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="view" label="浏览数量"></el-table-column>
      <el-table-column align="center" prop="msgs" label="评论">
        <template slot-scope="scope">
          <span class="btn text-blue" @click="lookComment(scope.row)">查看({{scope.row.msgs}})</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="createTime" label="创建时间"></el-table-column>
      <!--<el-table-column align="center" prop="updateTime" label="更新时间"></el-table-column>-->
      <el-table-column align="center" prop="isShow" label="是否展示">
        <template slot-scope="scope">
          <el-switch
            @change="changeIsShow(scope.row)"
            v-model="scope.row.isShow"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <span class="btn text-red" @click="removeArticle(scope.row)">删除</span>
          <span class="btn text-blue" @click="lookDetail(scope.row)">编辑</span>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="page-wrap"
      background
      @current-change="pageChange"
      :current-page.sync="pageNum"
      :page-size="pageSize"
      layout="prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
  import {queryAdminArticleList, updateArticleIsShow, deleteArticle} from "../../assets/api/getData";

  export default {
    name: "articleManage",
    data() {
      return {
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 0,
        keyWord: "",
      }
    },
    created() {
      this.getList();
    },
    methods: {
      onSearch() {

      },
      addArticle() {
        this.$router.push({
          path: "/articleDetail",
          query: {isUpdate: false}
        });
      },
      changeIsShow(row) {
        updateArticleIsShow({
          id: row.id,
          isShow: row.isShow
        }).then(() => {
          this.$message.success("设置成功");
          this.getList();
        }).catch(() => {
          row.isShow = row.isShow == 0 ? 1 : 0;
        })
      },
      removeArticle(row) {
        this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          let res = await deleteArticle({id: row.id});
          this.$message.success(res.message);
          this.getList();
        }).catch(() => {
          this.$message.info('已取消删除');
        });
      },
      lookComment(row) {
        this.$router.push({
          path: "/comment",
          query: {id: row.id}
        });
      },
      lookDetail(row) {
        this.$router.push({
          path: "/articleDetail",
          query: {id: row.id, isUpdate: true}
        });
      },
      pageChange() {
        this.getList();
      },
      async getList() {
        let res = await queryAdminArticleList({
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          classify: "",
        });
        this.tableData = res.data.rows;
        this.total = res.data.total;
      }
    }
  }
</script>

<style scoped>

</style>
